<template>
  <div class="app-container">
    <div class="filter-container">
      <el-date-picker v-model="listQuery.startTime" placeholder="开始时间" 
        type="datetime"
         style="width : 200px;"  class="search-input" />
      <el-date-picker v-model="listQuery.endTime" placeholder="结束时间" 
        type="datetime"
         style="width : 200px;"  class="search-input" />

      <el-select v-model="listQuery.peerId" placeholder="分公司" clearable style="margin-bottom : 0 ;" class="search-input">
        <el-option v-for="item in peerOptions" :key="item.peerId" :label="item.name" :value="item.peerId" />
      </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-download" @click="handleExport">
        导出
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <!--      <el-table-column label="线路ID" width="80px" align="center">-->
      <!--        <template slot-scope="{row}">-->
      <!--          <span>{{ row.ID }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="序号" align="center" width="50px">
        <template slot-scope="row">
          <span>{{ row.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分公司名称" align="center">
        <template slot-scope="{row}">
          <span v-html="row.peer" />
        </template>
      </el-table-column>
      <el-table-column label="分公司电话" align="center">
        <template slot-scope="{row}">
          <span v-html="row.mobile" />
        </template>
      </el-table-column>
      <el-table-column label="业务员数量" align="center">
        <template slot-scope="{row}">
          <span v-html="row.salesAmount" />
        </template>
      </el-table-column>
      <el-table-column label="分公司会员数量" align="center">
        <template slot-scope="{row}">
          <span v-html="row.userAmount" />
        </template>
      </el-table-column>
      <el-table-column label="下单会员数量" align="center">
        <template slot-scope="{row}">
          <span v-html="row.boughtAmount" />
        </template>
      </el-table-column>
      <el-table-column label="时间区间" align="center">
        <template slot-scope="{row}">
          <span style="color:blue">{{ listQuery.startTime | parseTime('{y}-{m}-{d}') }} - </span>
          <span style="color:blue">{{  listQuery.endTime | parseTime('{y}-{m}-{d}') }} </span>
        </template>
      </el-table-column>

    </el-table>
  </div>
</template>

<script>
import { list } from '@/api/userDataManagement'
import { listSelection } from '@/api/selectOptions'
export default {
  components: {
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        mobile: undefined, // 分公司电话
        salesAmount: undefined, // 业务员数量
        userAmount: undefined, // 分公司会员数量
        boughtAmount: undefined, // 下单会员数量
        peerId: undefined,
        csv: undefined // 为1时，导出
      },
      peerOptions: undefined
    }
  },
  created() {
    this.getPeerOptions()
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      list(this.listQuery).then(response => {
        this.list = response.data
        this.total = response.total
        this.listLoading = false
      })
    },
    getPeerOptions() {
      listSelection().then(response => {
        this.peerOptions = response.data
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleExport() {
      list(Object.assign(this.listQuery, { csv: 1 }), 'arraybuffer').then(response => {
        const content = response
        // 组装a标签
        const elink = document.createElement('a')
        // 设置下载文件名
        elink.download = '附件.csv'
        elink.style.display = 'none'
        const blob = new Blob([content], { type: '.csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel' })
        elink.href = URL.createObjectURL(blob)
        document.body.appendChild(elink)
        elink.click()
        document.body.removeChild(elink)
        this.listQuery.csv = 0
      })
    }
  }
}
</script>

<style>
.search-input {
  width: 150px;
  margin-right: 15px;
}
</style>
